package com.sh.data.engine.web.webapi.controller.api;

import com.sh.data.engine.application.model.dto.api.DataApiBaseResponse;
import com.sh.data.engine.application.model.dto.api.DataApiLogResponse;
import com.sh.data.engine.application.model.dto.api.DataApiResponse;
import com.sh.data.engine.application.model.dto.api.DataApiResponse.DataApiParamResponse;
import com.sh.data.engine.application.model.dto.common.DirectoryResponse;
import com.sh.data.engine.application.service.api.DataApiMarketApplicationService;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.api.model.domain.rest.DataApiTaskParamDomain;
import com.sh.data.engine.domain.api.service.DataApiAuthService;
import com.sh.data.engine.domain.api.service.DataApiService;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.common.service.DirectoryService;
import com.sh.data.engine.domain.util.ConvertUtil;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : fuchen
 * @date : 2022/1/24 18:02
 */
@RestController
@RequestMapping("/apiMarket")
@Tag(name = "数据服务-API市场", description = "数据服务-PI市场")
public class DataApiMarketController {

    @Autowired
    private DataApiService dataApiService;
    @Autowired
    private DirectoryService directoryService;
    @Autowired
    private DataApiAuthService dataApiAuthService;

    @Autowired
    private DataApiMarketApplicationService dataApiMarketApplicationService;

    @Operation(summary = "获取api组")
    @GetMapping("/getApiGroup")
//    @AuthorityAnnotation(menuId = MenuEnumNew.APISC, readWriteStatus = 0)
    public RestResponseEntity<List<DirectoryResponse>> getDataApiGroupList() {
        var result = dataApiMarketApplicationService.getDataApiGroupList();
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "获取列表")
    @GetMapping("/getMarketList")
//    @AuthorityAnnotation(menuId = MenuEnumNew.APISC, readWriteStatus = 0)
    public RestResponseEntity<PageResult<DataApiBaseResponse>> getMarketApiList(
        @RequestParam(name = "groupId", value = "groupId", required = false) String groupId,
        @RequestParam(name = "apiName", value = "apiName", required = false) String apiName,
        @RequestParam(name = "pageNum", value = "pageNum", required = false) Integer pageNum,
        @RequestParam(name = "pageSize", value = "pageSize", required = false) Integer pageSize) {
        var result = dataApiMarketApplicationService.getMarketApiList(groupId, apiName, pageNum, pageSize);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "获取api测试信息", description = "获取api测试信息")
    @GetMapping("/getApiTestInfo")
//    @AuthorityAnnotation(menuId = MenuEnumNew.APISC, readWriteStatus = 0)
    public RestResponseEntity<List<DataApiParamResponse>> getApiTestInfo(
        @Parameter(description = "api id", required = true) @RequestParam Long apiId) {
        List<DataApiTaskParamDomain> testInfo = dataApiService.getTestInfo(apiId);
        List<DataApiParamResponse> dataApiParamResponses =
            ConvertUtil.copyProperties(testInfo, DataApiParamResponse.class);
        return new RestResponseEntity<>(dataApiParamResponses);
    }

    @Operation(summary = "申请使用")
    @PostMapping("/applyApiById")
    @LogExecution(operation = EngineOperation.APPLYFOR, business = Business.数据服务)
//    @Log(
//        business = Business.数据服务,
//        operation = Operation.APPLYFOR)
//    @AuthorityAnnotation(menuId = MenuEnumNew.APISC, readWriteStatus = 1)
    public RestResponseEntity<Boolean> applyApiById(Long apiId) {
        var result = dataApiMarketApplicationService.applyApiById(apiId);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "批量申请使用")
    @PostMapping("/applyApiByIds")
    @LogExecution(operation = EngineOperation.APPLYFOR, business = Business.数据服务)
//    @Log(
//        business = Business.数据服务,
//        operation = Operation.APPLYFOR)
//    @AuthorityAnnotation(menuId = MenuEnumNew.APISC, readWriteStatus = 1)
    public RestResponseEntity<Boolean> applyApiByIds(String ids) {
        var result = dataApiMarketApplicationService.applyApiByIds(ids);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getDataApiDetailById")
    @Operation(summary = "获取api详情")
//    @AuthorityAnnotation(menuId = MenuEnumNew.APISC, readWriteStatus = 0)
    RestResponseEntity<DataApiResponse> getDataApiDetailById(Long id) {
        var result = dataApiMarketApplicationService.getDataApiDetailById(id);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "测试API")
    @PostMapping("/testApi")
//    @AuthorityAnnotation(menuId = MenuEnumNew.APISC, readWriteStatus = 1)
    public RestResponseEntity<Long> testApi(
        @RequestParam(name = "apiId", required = true) Long apiId, HttpServletRequest request) {
        var result = dataApiMarketApplicationService.testApi(apiId, request);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/log")
    @Operation(summary = "查看日志")
//    @AuthorityAnnotation(menuId = MenuEnumNew.APISC, readWriteStatus = 0)
    public RestResponseEntity<DataApiLogResponse> getRequestLog(
        @Parameter(description = "请求id", required = true) @RequestParam("logId") Long logId,
        @Parameter(description = "偏移量", required = true) @RequestParam("offset") Integer offset) {
        var result = dataApiMarketApplicationService.getRequestLog(logId, offset);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/exportApi")
    @Operation(summary = "导出api，apiId参数不传时为批量导出")
//    @Log(
//        business = Business.数据服务,
//        operation = Operation.DATA_API_EXPORT)
//    @AuthorityAnnotation(menuId = MenuEnumNew.APISC, readWriteStatus = 1)
    public void exportApi(
        @Parameter(description = "以逗号割开", required = true) @RequestParam String apiIds,
        HttpServletResponse response) {
        dataApiMarketApplicationService.exportApi(apiIds, response);
    }
}
